package com.wqh.zeromq.demo;
import org.zeromq.SocketType;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;
/**
 * 使用见简单的请求-应答模式
 * 请求必须是客户端发起,服务端响应
 */
import java.util.logging.Logger;

public class HWServer {
    private static Logger logger = Logger.getLogger(HWClient.class.getSimpleName());

    public static void main(String[] args) {

        try(ZContext context = new ZContext()){
            //创建“请求-应答模式”中“应答”的socket，使用其与客户端进行对话
            ZMQ.Socket socket = context.createSocket(SocketType.REP);
            //进行通信地址，创建线程进行通信
            socket.bind("tcp://*:5555");
            logger.info("服务端启动");
            //判断当前线程是否堵塞
            while(!Thread.currentThread().isInterrupted()){
                //阻塞当前线程直到收到客户端发送返回一条消息
                byte[] reply = socket.recv(0);

                System.out.println(">>:"+new String(reply,ZMQ.CHARSET));

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // Send a response
                socket.send("Hello, world!".getBytes(ZMQ.CHARSET), 0);
            }
        }
    }
}
